﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpriteBuilder
{
    [Flags]
    enum MouseCondition
    {
        leftClick = 0x01,
        rightClick = 0x02,
        holdleft = 0x04,
        holdright = 0x08,
        doubleleftClick = 0x10,
        leftPress = 0x20
    }
    class InputManager
    {
        MouseCondition oldMouseCondition;
        MouseState oldMouseState;
        Camera2D Camera;
        public KeyboardState KeyboardState;
        public MouseState presentMouseState;
        int elapesedHoldTime;
        int elapsedDoubleTime;
        int Holdtime;
        int DoubleTime;
        public MouseCondition presentMouseCondition;
        public Vector2 TransformedMouseCoords;
        public Vector2 TransformedOldMouseCoords;



        public InputManager(Camera2D camera)
        {
            oldMouseState = new MouseState();
            oldMouseCondition = new MouseCondition();
            elapesedHoldTime = 0;
            elapsedDoubleTime = 0;
            Holdtime = 20;
            DoubleTime = 200;
            Camera = camera;
        }

        public MouseCondition Update(GameTime time, MouseState newMouseState, KeyboardState keyboardstate, GraphicsDevice graphicsdevice)
        {
            KeyboardState = keyboardstate;
            presentMouseState = newMouseState;
            MouseCondition res = new MouseCondition();

            TransformedOldMouseCoords = TransformedMouseCoords;
            TransformedMouseCoords = new Vector2(presentMouseState.X, presentMouseState.Y);
            TransformedMouseCoords = Vector2.Transform(TransformedMouseCoords, Matrix.Invert(Camera.getTransform(graphicsdevice)));

            if (newMouseState.LeftButton == ButtonState.Released)
            {
                elapesedHoldTime = 0;
                if(oldMouseState.LeftButton == ButtonState.Pressed)
                    if ((oldMouseCondition == MouseCondition.leftClick || oldMouseCondition == MouseCondition.holdleft) && elapsedDoubleTime < DoubleTime)
                        res = MouseCondition.doubleleftClick;
                    else
                    {
                            res = MouseCondition.leftClick;
                            elapsedDoubleTime = 0;
                    }
            }

            if (newMouseState.RightButton == ButtonState.Released)
            {
                if (oldMouseState.RightButton == ButtonState.Pressed)
                    res = MouseCondition.rightClick;
            }

            if (newMouseState.LeftButton == ButtonState.Pressed)
            {
                if (elapesedHoldTime > Holdtime)
                    res = MouseCondition.holdleft;
                else
                    if(oldMouseCondition != MouseCondition.leftPress)
                    res = MouseCondition.leftPress;
                elapesedHoldTime += time.ElapsedGameTime.Milliseconds;                
            }

            elapsedDoubleTime += time.ElapsedGameTime.Milliseconds;

            if (newMouseState.LeftButton != oldMouseState.LeftButton ||
                newMouseState.RightButton != oldMouseState.RightButton)
            {
                oldMouseState = newMouseState;
            }
            if (res != 0)
                oldMouseCondition = res;
            presentMouseCondition = res;
            
            return res;
        }
    }
}
